package com.cmw.auntapp.component;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.cmw.auntapp.R;
import com.cmw.auntapp.listener.BackListener;

/**
 * 自定义标题栏
 * @Auth chengmingwei
 * @date 2021-04-05 16:48
 */
public class TitleBar extends LinearLayout {
    private Context context;
    private TextView tb_tv_title;
    private Button btnBack;
    private  Button btnEdit;

    private BackListener backListener;

    public TitleBar(final Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.title_bar, this);
        init();
    }

    public void init() {
        tb_tv_title = findViewById(R.id.tb_tv_title);
        btnBack = findViewById(R.id.tb_btn_back);
        btnEdit = findViewById(R.id.tb_btn_edit);
        btnBack.setOnClickListener( v -> {
            if (backListener != null) {
                backListener.execute(v);
            } else {
                ((Activity)getContext()).finish();
            }
        });

        btnEdit.setOnClickListener( v -> {
            Toast.makeText(context, "edit", Toast.LENGTH_SHORT).show();
        });
    }

    /**
     * 设置标题
     * @param resId 标题对应的资源ID
     */
    public void setTitle (int resId) {
        tb_tv_title.setText(resId);
    }

    /**
     * 设置标题
     * @param title 标题名称
     */
    public void setTitle (String title) {
        tb_tv_title.setText(title);
    }

    public void isShowBack(boolean showBack) {
        int visibility = showBack ? View.VISIBLE : View.INVISIBLE;
        btnBack.setVisibility(visibility);
    }

    /**
     * 自定义返回事件
     * @param backListener
     */
    public void setBackListener(BackListener backListener) {
        this.backListener = backListener;
    }
}
